<?php
/**
 * BootAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using BootAdmin.
 *
 * @Author G.r<root@BootAdmin.com>
 */

declare (strict_types=1);

namespace app\admin\validate;

use app\common\model\DictType;
use app\common\validate\BaseValidate;

/**
 * 字典分类验证器
 */
class DictTypeValidate extends BaseValidate
{
    protected $rule = [
        'id' => 'require|checkDict',
        'name' => 'require|length:5,32|unique:'.DictType::class,
        'code' => 'require|length:1,64|unique:'.DictType::class,
        'status' => 'require|in:0,1|checkAbleDisable'
    ];

    protected $message = [
        'id.require' => '分类id不能为空',
        'name.require' => '分类名称不能为空',
        'name.length' => '分类名称长度须在5-32位字符',
        'name.unique' => '分类名称已存在',
        'status.require' => '请选择状态',
        'status.in' => '状态值错误'
    ];

    /**
     * 添加场景
     * @return DictTypeValidate
     */
    public function sceneAdd()
    {
        return $this->remove('id', 'require|checkDict')
            ->remove('status', 'checkAbleDisable');
    }

    /**
     * 详情场景
     * @return DictTypeValidate
     */
    public function sceneDetail()
    {
        return $this->only(['id']);
    }

    /**
     * 编辑场景
     * @return DictTypeValidate
     */
    public function sceneEdit()
    {
        return $this->append('id', 'require|checkDict');
    }


    /**
     * 删除场景
     * @return DictTypeValidate
     */
    public function sceneDelete()
    {
        return $this->only(['id']);
    }

    /**
     * 恢复场景
     * @return DictTypeValidate
     */
    public function sceneRecovery()
    {
        return $this->only(['id']);
    }

    /**
     * 检查指定分类是否存在
     * @param $value
     * @return bool|string
     */
    public function checkDict($value)
    {
        $role = DictType::whereIn('id', $value)->withTrashed()->select();
        if ($role->isEmpty()) {
            return '分类不存在';
        }
        return true;
    }


    /**
     * 禁用校验
     * @param $value
     * @param $rule
     * @param $data
     * @return bool|string
     */
    public function checkAbleDisable($value, $rule, $data)
    {
        $role = DictType::whereIn('id', $data['id'])->select();
        if ($role->isEmpty()) {
            return '分类不存在';
        }
        return true;
    }
}